package com.lms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lms.entity.Pubbuildbaseinfo;
import com.lms.mapper.PubbuildbaseinfoMapper;
import com.lms.service.PubbuildbaseinfoService;
import com.lms.vo.DataVo;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 公共楼宇基础信息业务逻辑层接口
 * @author: L-B-S
 * @Date: 2021/3/12 17:11
 * @modified_By:
 * @Version: 0.0$
 */
@Service
public class PubbuildbaseinfoServiceImpl implements PubbuildbaseinfoService {

    @Autowired
    private PubbuildbaseinfoMapper pubbuildbaseinfoMapper;

    @Override
    public DataVo<Pubbuildbaseinfo> getList(Integer page, Integer limit) {
        DataVo<Pubbuildbaseinfo> dataVo = new DataVo<>();

        dataVo.setCode(0);
        dataVo.setMsg("");

        IPage<Pubbuildbaseinfo> pubbuildbaseinfoPage = new Page<>(page, limit);
        IPage<Pubbuildbaseinfo> result = pubbuildbaseinfoMapper.selectPage(pubbuildbaseinfoPage, null);

        dataVo.setCount(result.getTotal());
        dataVo.setData(result.getRecords());
        return dataVo;
    }

    @Override
    public DataVo<Pubbuildbaseinfo> delPbbis(List<Pubbuildbaseinfo> pbbiList) {
        DataVo<Pubbuildbaseinfo> dataVo = new DataVo<>();
        List<Long> ids = new ArrayList<>();
        if (pbbiList.size() == 0) {
            dataVo.setCode(1);
            dataVo.setMsg("您没有选择删除的行，请选择后操作");
        } else {
            for (Pubbuildbaseinfo pubbuildbaseinfo : pbbiList) {
                ids.add(pubbuildbaseinfo.getId());
            }
            pubbuildbaseinfoMapper.deleteBatchIds(ids);
            dataVo.setCode(0);
            dataVo.setMsg("删除成功，正在刷新数据，请稍等");
        }
        return dataVo;
    }

    @Override
    public DataVo<Pubbuildbaseinfo> delOne(Pubbuildbaseinfo pubbuildbaseinfo) {
        DataVo<Pubbuildbaseinfo> dataVo = new DataVo<>();
        int i = pubbuildbaseinfoMapper.deleteById(pubbuildbaseinfo.getId());
        if (i > 0) {
            dataVo.setCode(0);
            dataVo.setMsg("删除行成功");
        } else {
            dataVo.setCode(1);
            dataVo.setMsg("网络异常，删除失败");
        }
        return dataVo;
    }

    @Override
    public DataVo<Pubbuildbaseinfo> addOne(Pubbuildbaseinfo pubbuildbaseinfo) {
        DataVo<Pubbuildbaseinfo> dataVo = new DataVo<>();
        Pubbuildbaseinfo pbbi = pubbuildbaseinfoMapper.selectByPubBuild(pubbuildbaseinfo.getPubBuild());
        if (pbbi != null) {
            dataVo.setCode(1);
            dataVo.setMsg("此编号的宿舍楼宇已存在，请修改后再试");
        } else {
            pubbuildbaseinfoMapper.insert(pubbuildbaseinfo);
            dataVo.setCode(0);
            dataVo.setMsg("新增数据成功，正在刷新数据，请稍等...");
        }
        return dataVo;
    }

    @Override
    public DataVo<Pubbuildbaseinfo> edit(Pubbuildbaseinfo pubbuildbaseinfo) {
        DataVo<Pubbuildbaseinfo> dataVo = new DataVo<>();
        Pubbuildbaseinfo obj = pubbuildbaseinfoMapper.selectById(pubbuildbaseinfo.getId());
        if (obj.equals(pubbuildbaseinfo)) {
            dataVo.setCode(1);
            dataVo.setMsg("您还未修改任何一条属性，请修改后提交");
        } else {
            pubbuildbaseinfoMapper.updateById(pubbuildbaseinfo);
            dataVo.setCode(0);
            dataVo.setMsg("修改成功，正在刷新页面，请稍等...");
        }
        return dataVo;
    }

    @Override
    public DataVo<Pubbuildbaseinfo> uploadExcel(MultipartFile file) {
        DataVo<Pubbuildbaseinfo> dataVo = new DataVo<>();
        try {
            String filename = file.getOriginalFilename();
            System.out.println(filename);
            InputStream inputStream = file.getInputStream();
            final XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            XSSFSheet sheet = workbook.getSheetAt(0);
            Row headRow = sheet.getRow(0);
            if ("校区".equals(headRow.getCell(0).getStringCellValue()) && "公共楼宇".equals(headRow.getCell(1).getStringCellValue()) && "面积".equals(headRow.getCell(2).getStringCellValue()) && "建设时间".equals(headRow.getCell(3).getStringCellValue()) && "投用时间".equals(headRow.getCell(4).getStringCellValue()) && "房屋结构".equals(headRow.getCell(5).getStringCellValue()) && "屋面面积（㎡）".equals(headRow.getCell(6).getStringCellValue()) && "外立面材质".equals(headRow.getCell(7).getStringCellValue()) && "办公室（间）".equals(headRow.getCell(8).getStringCellValue()) && "教室（间）".equals(headRow.getCell(9).getStringCellValue()) && "实验室（间）".equals(headRow.getCell(10).getStringCellValue()) && "设计（绘图）室（间）".equals(headRow.getCell(11).getStringCellValue()) && "教休室（间）".equals(headRow.getCell(12).getStringCellValue()) && "多媒体教室（间）".equals(headRow.getCell(13).getStringCellValue()) && "标准化考场（间）".equals(headRow.getCell(14).getStringCellValue()) && "座位数（个）".equals(headRow.getCell(15).getStringCellValue()) && "卫生间（间）".equals(headRow.getCell(16).getStringCellValue()) && "水龙头（个）".equals(headRow.getCell(17).getStringCellValue()) && "感应器（个）".equals(headRow.getCell(18).getStringCellValue()) && "冲水阀（个）".equals(headRow.getCell(19).getStringCellValue()) && "照明灯（个）".equals(headRow.getCell(20).getStringCellValue()) && "饮水机（个）".equals(headRow.getCell(21).getStringCellValue()) && "配电箱（个）".equals(headRow.getCell(22).getStringCellValue()) && "配电室（个）".equals(headRow.getCell(23).getStringCellValue()) && "消防门（个）".equals(headRow.getCell(24).getStringCellValue()) && "消防栓（个）".equals(headRow.getCell(25).getStringCellValue()) && "暖气井（个）".equals(headRow.getCell(26).getStringCellValue()) && "电梯（部）".equals(headRow.getCell(27).getStringCellValue())) {
                int index = 1;
                boolean flag = true;
                List<Pubbuildbaseinfo> pbbiList = new ArrayList<>();
                while (flag) {
                    Row row = sheet.getRow(index++);
                    if (row != null) {
                        Pubbuildbaseinfo pubbuildbaseinfo = new Pubbuildbaseinfo();
                        pubbuildbaseinfo.setLocation(String.valueOf(row.getCell(0)));
                        pubbuildbaseinfo.setPubBuild(String.valueOf(row.getCell(1)));
                        pubbuildbaseinfo.setArea(Double.parseDouble(row.getCell(2).getStringCellValue()));
                        pubbuildbaseinfo.setBuildTime(Long.parseLong(row.getCell(3).getStringCellValue()));
                        pubbuildbaseinfo.setUseTime(Long.parseLong(row.getCell(4).getStringCellValue()));
                        pubbuildbaseinfo.setBuildStru(String.valueOf(row.getCell(5)));
                        pubbuildbaseinfo.setRoofArea(Double.parseDouble(row.getCell(6).getStringCellValue()));
                        pubbuildbaseinfo.setFacadeMaterial(String.valueOf(row.getCell(7)));
                        pubbuildbaseinfo.setOfficeNum(Long.parseLong(row.getCell(8).getStringCellValue()));
                        pubbuildbaseinfo.setClassNum(Long.parseLong(row.getCell(9).getStringCellValue()));
                        pubbuildbaseinfo.setLabNum(Long.parseLong(row.getCell(10).getStringCellValue()));
                        pubbuildbaseinfo.setDesignNum(Long.parseLong(row.getCell(11).getStringCellValue()));
                        pubbuildbaseinfo.setTeaRestNum(Long.parseLong(row.getCell(12).getStringCellValue()));
                        pubbuildbaseinfo.setMulMediaNum(Long.parseLong(row.getCell(13).getStringCellValue()));
                        pubbuildbaseinfo.setStandardExamNum(Long.parseLong(row.getCell(14).getStringCellValue()));
                        pubbuildbaseinfo.setSeatNum(Long.parseLong(row.getCell(15).getStringCellValue()));
                        pubbuildbaseinfo.setToiletNum(Long.parseLong(row.getCell(16).getStringCellValue()));
                        pubbuildbaseinfo.setFaucetNum(Long.parseLong(row.getCell(17).getStringCellValue()));
                        pubbuildbaseinfo.setInductorNum(Long.parseLong(row.getCell(18).getStringCellValue()));
                        pubbuildbaseinfo.setFlashNum(Long.parseLong(row.getCell(19).getStringCellValue()));
                        pubbuildbaseinfo.setLightNum(Long.parseLong(row.getCell(20).getStringCellValue()));
                        pubbuildbaseinfo.setWaterNum(Long.parseLong(row.getCell(21).getStringCellValue()));
                        pubbuildbaseinfo.setSwitchBoxNum(Long.parseLong(row.getCell(22).getStringCellValue()));
                        pubbuildbaseinfo.setSwitchRoomNum(Long.parseLong(row.getCell(23).getStringCellValue()));
                        pubbuildbaseinfo.setFireDoorNum(Long.parseLong(row.getCell(24).getStringCellValue()));
                        pubbuildbaseinfo.setFireHydrantNum(Long.parseLong(row.getCell(25).getStringCellValue()));
                        pubbuildbaseinfo.setHeatWellNum(Long.parseLong(row.getCell(26).getStringCellValue()));
                        pubbuildbaseinfo.setElevatorNum(Long.parseLong(row.getCell(27).getStringCellValue()));
                        pbbiList.add(pubbuildbaseinfo);
                    } else {
                        flag = false;
                    }
                }
                inputStream.close();
                if (!pbbiList.isEmpty()) {
                    for (Pubbuildbaseinfo pubbuildbaseinfo : pbbiList) {
                        if (pubbuildbaseinfoMapper.selectById(pubbuildbaseinfo.getId()) != null) {
                            dataVo.setCode(2);
                            dataVo.setMsg("文件内容为空或有多行数据重复，请检查后导入");
                        } else {
                            pubbuildbaseinfoMapper.insert(pubbuildbaseinfo);
                            dataVo.setCode(0);
                            dataVo.setMsg("导入数据成功，正在刷新数据，请稍等...");
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataVo;
    }
}
